package org.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.erp.domain.ProductMaterialManagement;
import org.example.erp.mapper.MaterialMapper;
import org.example.erp.service.ProductMaterialManagementService;
import org.example.erp.mapper.ProductMaterialManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 86180
 * @description 针对表【product_material_management(物料产品管理表)】的数据库操作Service实现
 * @createDate 2024-11-12 16:25:26
 */
@Service
public class ProductMaterialManagementServiceImpl extends ServiceImpl<ProductMaterialManagementMapper, ProductMaterialManagement>
        implements ProductMaterialManagementService {
    @Autowired
    private ProductMaterialManagementMapper productMaterialManagementMapper;
    @Autowired
    private MaterialMapper materialMapper;

    public IPage<ProductMaterialManagement> find(
            Integer currentPage,
            Integer pageSize,
            String materialName,
            String productName,
            String materialProductCode,
            Integer materialId) {
        Page<ProductMaterialManagement> page = new Page<>(currentPage, pageSize);
        List<Integer> list = new ArrayList<>();
        if (materialId != null) {
            list = materialMapper.findMaterialIds(materialId);
            list.add(materialId);
            return productMaterialManagementMapper.find(page, materialName, productName, materialProductCode, list);
        }
        return productMaterialManagementMapper.find(page, materialName, productName, materialProductCode, list);
    }


    public IPage<ProductMaterialManagement> findboms(
            Integer currentPage,
            Integer pageSize,
            String materialProductCode
    ) {
        Page<ProductMaterialManagement> page = new Page<>(currentPage, pageSize);
        return productMaterialManagementMapper.findboms(page, materialProductCode);
    }

    // 只返回产品信息
    public List<ProductMaterialManagement> findProducts(){
        List<Integer> materialIds = materialMapper.findMaterialIds(2); //2为产品
        LambdaQueryWrapper<ProductMaterialManagement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductMaterialManagement::getMaterialId, materialIds);
        return productMaterialManagementMapper.selectList(queryWrapper);
    }

}


